package com.eportal.service.impl;

import com.eportal.base.BaseDAO;
import com.eportal.dao.TOpenlogEntityMapper;
import com.eportal.domain.Sysopt;
import com.eportal.domain.TMirrorKeys;
import com.eportal.domain.TOpenLog;
import com.eportal.domain.TUsers;
import com.eportal.entity.TOpenlogEntity;
import com.eportal.exception.NotUniqueException;
import com.eportal.params.background.TOpenlog.TOpenLogParamList;
import com.eportal.service.TOpenLogService;
import com.eportal.util.CityUtil;
import com.eportal.vo.controller.background.tlocks.TlockListSendVO;
import com.eportal.vo.controller.background.topenlog.TOpenLogListRow;
import com.eportal.vo.controller.background.topenlog.TOpenLogListVO;
import com.framework.jqgrid.JqGridPager;
import com.framework.jqgrid.PageEntity;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("tOpenLogService")
public class TOpenLogServiceImpl  implements TOpenLogService{

	@Resource(name="dao")
	private BaseDAO dao;

	@Resource
	private TOpenlogEntityMapper tOpenlogDao;

	private static Logger log = Logger.getLogger(TOpenLogServiceImpl.class.getName());
	@Override
	public TOpenLog get(Long sid){
		TOpenLog tOpenLog = (TOpenLog)dao.getById(TOpenLog.class, sid);
		return tOpenLog;
	}
	@Override
	public List<Map<String, Object>> list(JqGridPager jqGridPager) {
		String SQL = "select ab.* from("
				+ "select t.sid,t.aUid,t.status,t.pid,t.ktype,t.opentype,t.result,t.otime as otime1,t.agtcode,t.communityid,TO_CHAR (t.otime,'yyyy-MM-dd HH24:mi:ss') as otime,a.agtname,a.sysoptId,c.communityname,c.citycode,u.tel,u.auname,"
				+ "(select min(t1.utype) from t035_room_user t1 where t1.app_userid = t.auid and t1.room_id in (select t2.sid from t035_room t2 where t2.unit_id=l.installunitid)) utype,"
				+ "l.install_lock_name as installName,t.ksid "
				+ "from TOPENLOG t "
				+ "left join t02_agtent a on t.agtcode=a.agtcode "
				+ "left join t03_community c on t.communityid=c.sid "
				//+ "left join t035_room_user rs on t.auid=rs.app_userid "
				+ "left join t04_users u on t.auid=u.userid "
				+ "left join t05_locks l on t.pid=l.pid where t.ktype='0'"
				+ " union "
				+ "select t.sid,t.aUid,t.status,t.pid,t.ktype,t.opentype,t.result,t.otime as otime1,t.agtcode,t.communityid,TO_CHAR (t.otime,'yyyy-MM-dd HH24:mi:ss') as otime,a.agtname,a.sysoptId,c.communityname,c.citycode,u.tel,u.auname,"
				+ "(select min(t1.utype) from t035_room_user t1 where t1.app_userid = t.auid and t1.room_id in (select t2.sid from t035_room t2 where t2.unit_id=l.installunitid)) utype,"
				+ "l.install_lock_name as installName,t.ksid   "
				+ "from TOPENLOG t "
				+ "left join t02_agtent a on t.agtcode=a.agtcode "
				+ "left join t03_community c on t.communityid=c.sid "
				//+ "left join t035_room_user rs on t.auid=rs.app_userid "
				+ "left join "
				+ "(select u.tel,u.auname,u.userid,tmk.sid as tmksid  from t04_users u  left join tmirror_keys tmk on tmk.puid=u.userid )u "
				+ "on t.ksid=u.tmksid "
				+ "left join t05_locks l on t.pid=l.pid where t.ktype='1'"
				+ ") ab";
	     return dao.query(SQL, jqGridPager);
	}

	@Override
	public List<Map<String, Object>> exportExcelList(String where) {
		//OPENTYPE 0:访客微信开门记录;1:认证用户微信开门记录;2:app开门记录;3:指纹开门;4:密码开门；5：门禁卡；6:机械钥匙 //Created by Saber on 2017/8/9 上午11:20
		String SQL = "select ab.* from(" //todo Updated by Saber on 2017/8/9 上午11:21
				+ "select t.sid,t.aUid,t.pid,t.opentype,case t.opentype WHEN '6' THEN '机械钥匙' WHEN '5' THEN '门禁卡' WHEN '4' THEN '密码开门'  WHEN '3' THEN '指纹开门' when '2' then 'app开门记录' when '1' then '微信开门记录' when '0' then '访客微信开门记录' end  as opentype1 ,"
				+"t.otime as otime1,t.agtcode,t.communityid,t.result,"
				+ "TO_CHAR (t.otime,'yyyy-MM-dd HH24:mi:ss') as otime,a.agtname,c.communityname,"
				+ "c.citycode,u.tel,u.auname,l.install_lock_name as installName "
				+ "from TOPENLOG t "
				+ "left join t02_agtent a on t.agtcode=a.agtcode "
				+ "left join t03_community c on t.communityid=c.sid "
				+ "left join t04_users u on t.auid=u.userid "
				+ "left join t05_locks l on t.pid=l.pid"
				+ ") ab";
		 return dao.queryBySql(SQL+" where "+where);
	}
	@Override
	public TUsers getMirrotKeyInfo(Long sid) {

		TMirrorKeys mirrorKeys = (TMirrorKeys)dao.getById(TMirrorKeys.class, sid);
		if(mirrorKeys!=null){
			String puid = mirrorKeys.getpUid();
			try {
				TUsers user = (TUsers)dao.getUniqueFirst("from TUsers where userid='"+puid+"'");
				return user;
			} catch (NotUniqueException e) {
				log.error(this, e);
			}
		}
		return null;
	}



//todo -------------------------------------------------------------------------------------------------


	/**
	 * 开门日志列表
	 *
	 * @param pageEntity 分页对象
	 * @param param      参数
	 * @param user       当前用户
	 * @param userRole   当前用户userRole
	 * @return
	 * @author Saber
	 * @Date 2017/8/8 上午9:05
	 */
	@Override
	public PageInfo<TOpenLogListVO> queryForPage(PageEntity pageEntity, TOpenLogParamList param, Sysopt user, int userRole) {
		//执行分页
		PageHelper.startPage(pageEntity.getPageNo(), pageEntity.getPageSize());
		//排序
		String orderField = pageEntity.getSort();
		String orderDirection = pageEntity.getOrder();
		if (org.apache.commons.lang3.StringUtils.isNotEmpty(orderField)) {
			PageHelper.orderBy(orderField);
			if (org.apache.commons.lang3.StringUtils.isNotEmpty(orderDirection)) {
				PageHelper.orderBy(orderField + " " + orderDirection);
			}
		}
		//判断是否是直辖市
		boolean isMunicipalities = false;
		if (org.apache.commons.lang3.StringUtils.isNotBlank(param.getAreaCode())) {
			isMunicipalities = CityUtil.isMunicipalities(param.getAreaCode());
		}

		List<TOpenLogListVO> list = tOpenlogDao.queryForList(param,isMunicipalities,user, userRole);

		return new PageInfo<>(list);
	}

	/**
	 * 获取要导出的excel列表数据
	 *
	 * @param param    参数
	 * @param user     当前用户
	 * @param userRole 当前用户userRole
	 * @return
	 * @author Saber
	 * @Date 2017/8/9 下午12:02
	 */
	@Override
	public List<TOpenLogListRow> exportExcel(TOpenLogParamList param, Sysopt user, int userRole) {
		return tOpenlogDao.queryForExcelList(param,user, userRole);
	}
}
